using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        private List<LOGINFO> MapLOGINFO(XElement item2, bool mergedb)
        {
            List<LOGINFO> result = new List<LOGINFO>();
            var item4 = from item3 in item2.Elements()
                        select new LOGINFO
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            TIMESTAMP = (string)item3.Element("TIMESTAMP"),
                            STACK = (string)item3.Element("STACK"),
                            LOGTEXT = (string)item3.Element("LOGTEXT"),
                            Photo = (string)item3.Element("Photo")
                        }; 
            
            result = item4.ToList();
            MapLOGINFO(result, mergedb);
            return result;
        }
        private List<LOGINFO> MapLOGINFO(List<LOGINFO> result, bool mergedb)
        { 
            if (mergedb)
            {
                var rowsexist = from u in LocalDB.LOGINFO.ToList().AsQueryable()
                                join uc in result.AsQueryable() on u.MAINKEY equals uc.MAINKEY
                                select u;
                LocalDB.LOGINFO.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.LOGINFO.InsertAllOnSubmit(result);
                LocalDB.SubmitChanges();
            }

            return result;
        }
    }

    [Table(Name = "LOGINFO")]
    public class LOGINFO : INotifyPropertyChanged,INotifyPropertyChanging
    {
        public LOGINFO()
        { 
        }
        public LOGINFO(string message, string stack)
        {
            _TIMESTAMP = System.DateTime.Now.ToFileTime().ToString();
            _LOGTEXT = message;
            _STACK = stack;
        }
        private string _MAINKEY="0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    NotifyPropertyChanging("MAINKEY");
                     _MAINKEY = value;
                     NotifyPropertyChanged("MAINKEY");
                }
            }
        }
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _TIMESTAMP="0";
        [Column]
        public string TIMESTAMP
        {
            get
            {
                return _TIMESTAMP;
            }
            set
            {
                if (value != _TIMESTAMP)
                {
                    NotifyPropertyChanging("TIMESTAMP");
                     _TIMESTAMP = value;
                    NotifyPropertyChanged("TIMESTAMP");
                }
            }
        }
        private string  _STACK="0";
        [Column]
        public string STACK
        {
            get
            {
                return _STACK;
            }
            set
            {
                if (value != _STACK)
                {
                    NotifyPropertyChanging("STACK");
                     _STACK = value;
                    NotifyPropertyChanged("STACK");
                }
            }
        }
        private string  _LOGTEXT="0";
        [Column]
        public string LOGTEXT
        {
            get
            {
                return _LOGTEXT;
            }
            set
            {
                if (value != _LOGTEXT)
                {
                    NotifyPropertyChanging("LOGTEXT");
                     _LOGTEXT = value;
                    NotifyPropertyChanged("LOGTEXT");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<LOGINFO> ListInput)
        {           
            try
            {
                string localfile = "Data\\PhoneDB\\LOGINFO\\LOGINFO" + Guid.NewGuid().ToString() + ".xml";
                string folder = "Data\\PhoneDB\\LOGINFO" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("PhoneDB");
                //Add root to file
                xdoc.Add(root);
                foreach (LOGINFO row in ListInput)
                {
                    XElement f = new XElement("LOGINFO");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement TIMESTAMP = new XElement("TIMESTAMP");
                         TIMESTAMP.Value = row.TIMESTAMP;
                          f.Add(TIMESTAMP);
                        XElement STACK = new XElement("STACK");
                         STACK.Value = row.STACK;
                          f.Add(STACK);
                        XElement LOGTEXT = new XElement("LOGTEXT");
                         LOGTEXT.Value = row.LOGTEXT;
                          f.Add(LOGTEXT);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
        public bool IsFirst=false;
        private string _Photo = "YES";
        public string Photo
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    NotifyPropertyChanging("Photo");
                    _Photo = value;
                    NotifyPropertyChanged("Photo");
                    NotifyPropertyChanged("PhotoISOSource");
                }
            }
        }
        public string PhotoSource
        {
            get
            {
                string image = string.Format("/Data/Images/{0}", "YES");
                if (!Photo.Contains("http://"))
                {
                    return string.Format("/Data/Images/{0}", Photo);
                }
                return Photo;
            }
        }
    public BitmapImage PhotoISOSource
        {
            get
            {
                string image = string.Format("Data/PhoneDB/Pictures/{0}.jpg",Photo);
                BitmapImage bi = new BitmapImage();
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if(!iso.FileExists(image))
                    image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", "YES");
                using (IsolatedStorageFileStream fileStream = iso.OpenFile(image, FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                }              
                return bi;
            }
        }                                        
    }
}
